package com.xiaoying.server.publish.impl;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;

import com.xiaoying.server.publish.Handler;
import com.xiaoying.server.publish.entity.FileInfo;
import com.xiaoying.server.publish.entity.UploadResult;
import com.xiaoying.server.publish.service.UploadResultService;
import com.xiaoying.server.publish.task.ResultDealTask;

public class ResponseHandler implements Handler,Runnable {
    
    private static Log log = LogFactory.getFactory().getInstance(ResponseHandler.class);

	private int maxThread;
    private BlockingQueue<UploadResult> resQueue;
    private Executor executor;
	private UploadResultService uploadResultService;	
	private static UploadResultService uploadResultServiceStatic;	
	private AtomicInteger idleThread;
	private BlockingQueue<String> redisBackupQueue;
	
	public ResponseHandler(){
		
	}
    
    @Override
    public void handle(BlockingQueue<FileInfo> reqQueue,
            BlockingQueue<UploadResult> resQueue) {
        this.resQueue = resQueue;
        Thread proc = new Thread(this);
        proc.start();
    }

    @Override
    public void init() throws Exception {
        if (log.isInfoEnabled())
        	log.info("初始化上传结果处理程序...");
        idleThread = new AtomicInteger(maxThread);
        executor = Executors.newFixedThreadPool(maxThread);      
    }
    

    @Override
    public void run() {
        while (true) {
        	if(idleThread.get()>0){
        		System.out.println("---->结果处理线程池空闲线程数量="+idleThread.get()+", 结果处理队列数量="+resQueue.size());
                try {
                	UploadResult uploadResult = resQueue.take();                
//                    ResultDealTask task = new ResultDealTask(resQueue, uploadResult,
//                    											uploadResultServiceStatic,idleThread);
                    ResultDealTask task = new ResultDealTask(resQueue, uploadResult,
                		  						uploadResultServiceStatic,idleThread,redisBackupQueue);
                    executor.execute(task);
                } catch (InterruptedException e) {
                    log.error(e);
                }    
        	}
        }
    }

	public UploadResultService getUploadResultService() {
		return uploadResultService;
	}

	public void setUploadResultService(UploadResultService uploadResultService) {
		this.uploadResultService = uploadResultService;
		uploadResultServiceStatic = this.uploadResultService;
	}

	public Executor getExecutor() {
		return executor;
	}

	public void setExecutor(Executor executor) {
		this.executor = executor;
	}

    public int getMaxThread() {
		return maxThread;
	}

	public void setMaxThread(int maxThread) {
		this.maxThread = maxThread;
	}
	
	public void setBackupQueue(BlockingQueue<String> redisBackupQueue){
		this.redisBackupQueue = redisBackupQueue;
	}
}
